package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.compete;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ITCompeteTenderService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.*;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class DataScreenBu {
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private ITCompeteTenderService iTCompeteTenderService;

    /**
     * 最新中标
     *
     * @param token
     * @return
     * @throws Exception
     */
    public SzResult latestWinningBid(String token, String plantId) throws Exception {
        // String dataFrom = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        List<TCompeteTenderVo> list = iTCompeteTenderService.latestWinningBid(plantId);
        return SzResult.success(list);
    }

    /**
     * 投标客户分析(各公司投标次数)
     *
     * @param token
     * @return
     * @throws Exception
     */
    public SzResult bidCustomerAnalysis(String token, String plantId) throws Exception {
        //String dataFrom = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        List<BidCustomerAnalysisVo> list = iTCompeteTenderService.bidCustomerAnalysis(plantId);
        return SzResult.success(list);
    }


    /**
     * 中标客户分析(各公司中标次数)
     *
     * @param token
     * @return
     * @throws Exception
     */
    public SzResult winBidCustomerAnalysis(String token, String plantId) throws Exception {
        //String dataFrom = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        List<BidCustomerAnalysisVo> list = iTCompeteTenderService.winBidCustomerAnalysis(plantId);
        return SzResult.success(list);
    }


    /**
     * 客户分析
     */
    public SzResult customerAnalysis(String token, String plantId) throws Exception {
        // String dataFrom = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        List<CustomerAnalysisVo> list = iTCompeteTenderService.customerAnalysis(plantId);
        return SzResult.success(list);
    }


    /**
     * 平台招投标详情
     *
     * @param token
     * @return
     * @throws Exception
     */
    public SzResult platFormBiddingDetails(String token, String plantId) throws Exception {
        List<PlatFormBiddingDetailsVo> list = iTCompeteTenderService.platFormBiddingDetails(plantId);
        Map<String, String> map = new HashMap<>();

        // 统一处理空列表和索引越界
        String[] keys = {"thisYear", "lastYear", "beforeLastYear"};
        for (int i = 0; i < keys.length; i++) {
            String value = "0";
            if (list != null && list.size() > i && list.get(i) != null) {
                value = list.get(i).getCount();
            }
            map.put(keys[i], value);
        }
        return SzResult.success(map);
    }


    /**
     * 基准价平均值
     *
     * @param token
     * @return
     * @throws Exception
     */
    public SzResult basePriceMeanValue(String token, String plantId) throws Exception {
        //String dataFrom = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        List<BasePriceMeanValueVo> list = iTCompeteTenderService.basePriceMeanValue(plantId);
        return SzResult.success(list);
    }

    /**
     * 中标价对比情况
     */
    public SzResult comparisonOfBidPrices(String token, String plantId) throws Exception {
        // String dataFrom = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        List<ComparisonOfBidPricesVo> list = iTCompeteTenderService.comparisonOfBidPrices(plantId);

        return SzResult.success(list);
    }


    /**
     * 中标价趋势
     */

    public SzResult bidWinningPriceTrendChart(String token, String plantId) throws Exception {
        //String dataFrom = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        List<BidWinningTrendChartVo> list = iTCompeteTenderService.bidWinningTrendChart(plantId);
        Map<String, List<String>> groupedData = new HashMap<>();
// 遍历list集合
        for (BidWinningTrendChartVo item : list) {
            // 获取当前元素的产品名称
            String product = item.getProduct();
            // 如果已经存在该产品名称的分组，直接添加中标量到对应的List中
            if (groupedData.containsKey(product)) {
                List<String> winBidList = groupedData.get(product);
                winBidList.add(item.getWinBidPrice());
            }
            // 否则，创建一个新的List，并将中标量添加到其中，并将该List添加到对应产品名称的分组中
            else {
                List<String> winBidList = new ArrayList<>();
                winBidList.add(item.getWinBidPrice());
                groupedData.put(product, winBidList);
            }
        }
// 创建一个List来存放最终结果
        List<Map<String, Object>> result = new ArrayList<>();
// 封装为所需的格式
        for (String product : groupedData.keySet()) {
            Map<String, Object> item = new HashMap<>();
            item.put("product", product);
            item.put("price", groupedData.get(product));
            result.add(item);
        }
        return SzResult.success(result);
    }

    /**
     * 中标量趋势
     */
    public SzResult bidWinningWeightTrendChart(String token, String plantId) throws Exception {
        //String dataFrom = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        List<BidWinningTrendChartVo> list = iTCompeteTenderService.bidWinningTrendChart(plantId);
        Map<String, List<String>> groupedData = new HashMap<>();
// 遍历list集合
        for (BidWinningTrendChartVo item : list) {
            // 获取当前元素的产品名称
            String product = item.getProduct();
            // 如果已经存在该产品名称的分组，直接添加中标量到对应的List中
            if (groupedData.containsKey(product)) {
                List<String> winBidList = groupedData.get(product);
                winBidList.add(item.getWinBidWeight());
            }
            // 否则，创建一个新的List，并将中标量添加到其中，并将该List添加到对应产品名称的分组中
            else {
                List<String> winBidList = new ArrayList<>();
                winBidList.add(item.getWinBidWeight());
                groupedData.put(product, winBidList);
            }
        }
// 创建一个List来存放最终结果
        List<Map<String, Object>> result = new ArrayList<>();
// 封装为所需的格式
        for (String product : groupedData.keySet()) {
            Map<String, Object> item = new HashMap<>();
            item.put("product", product);
            item.put("winBid", groupedData.get(product));
            result.add(item);
        }
        return SzResult.success(result);
    }
}
